package com.attendancesystem.service;

import com.attendancesystem.model.Attendance;
import com.attendancesystem.model.AttendanceDetails;
import com.attendancesystem.util.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AttendanceDetailService {
    public List<AttendanceDetails> getAttendanceDetail() throws SQLException{
        String sql="select * from attendancedetails";
        List<AttendanceDetails> details=new ArrayList<>();

        try(Connection con= JDBCUtils.getConnection();

            PreparedStatement pstmt= con.prepareStatement(sql)){
            try(ResultSet rs=pstmt.executeQuery()) {
                while (rs.next()){
                    AttendanceDetails detail=new AttendanceDetails();
                    detail.setAttendanceDetailId(rs.getInt("attendanceDetail_id"));

                    Timestamp timestamp = rs.getTimestamp("attendanceDetail_time");
                    detail.setAttendanceDetailTime(timestamp != null ?
                            timestamp.toLocalDateTime() : null);

                    detail.setAttendanceDetailClass(rs.getString("attendanceDetail_class"));
                    detail.setAttendanceDetailCourse(rs.getString("attendanceDetail_course"));
                    detail.setAttendanceDetailPerson(rs.getInt("attendanceDetail_person"));
                    detail.setAttendanceDetailActual(rs.getInt("attendanceDetail_actual"));
                    detail.setAttendanceDetailRate(rs.getBigDecimal("attendanceDetail_rate"));
                    detail.setAttendanceDetailEnum(AttendanceDetails.AttendanceStatus.AttendanceDetailFromChineseName(rs.getString("attendanceDetail_enum")));
                    details.add(detail);

                }



            }


//            int rows=pstmt.executeUpdate();
//            if (rows>0){
//                System.out.println("查询成功");
//            }

        }catch (SQLException e){
            e.printStackTrace();
        }
        return details;
    }
    public List<Attendance> getAttendance(String courseName, String attendanceDate) throws SQLException{
        String sql="select * from attendance where course_name=? and attendance_date=?";
        List<Attendance> attendances=new ArrayList<>();
        try(Connection con= JDBCUtils.getConnection();

            PreparedStatement pstmt= con.prepareStatement(sql)){
            pstmt.setString(1,courseName);
            pstmt.setDate(2, Date.valueOf(attendanceDate));
            try(ResultSet rs=pstmt.executeQuery()) {
                while (rs.next()){
                    Attendance attendance=new Attendance();
                    attendance.setAttendanceId(rs.getInt("attendance_id"));
                    attendance.setStudentId(rs.getString("student_id"));
                    attendance.setCourseName(rs.getString("course_name"));
                    attendance.setAttendanceDate(rs.getDate("attendance_date").toLocalDate());
                    attendance.setStatus(Attendance.Status.fromChineseName(rs.getString("status")));
                    attendances.add(attendance);

                }

            }



        }catch (SQLException e){
            e.printStackTrace();
        }
        return attendances;

    }
}
